<?php
namespace Common\Model;
use Think\Model;
class CommonModel extends Model{
    
    /*展示功能  */
    public function ShowTable($table, $where='', $start=0, $end= 10){
        $end= $end ? $this->CountTable($table) : $end;
        $show = M($table)->limit($start,$end)->where($where)->order('id desc')->select();
        return $show;
    }

    /* 展示几个字段 */
    public function ShowColumns($table,$field,$where='',$order='',$start=0,$end=''){
        $end=($end=='')?$this->CountTable($table):$end;
        $order=($order=='')?'id':$order;
        if(is_array($field)){
            //如果是数组
            foreach($field as $v){
                if(empty($str)){
                    $str = $v;
                }else{
                    $str.=','.$v;
                }
            }
        }else{
            $str = $field;
        }
        $show = M($table)->limit($start,$end)->field($str)->where($where)->order($order)->select();
        return $show;
    }

    //查一条数据
    public function ShowFind($table,$where){
       $info= M($table)                
                  ->field('images')
                   ->group('userId')
                   ->where("userId=$where")
                  ->select();
       return $info;
    }

    /* 多表联查 */
    public function MoreTable($table,$field,$where){
        //传表  传关联字段，传要查出的字段，
        $count = count($table);
        $sql = M($table[0][0]);
        for($i=0;$i<$count;$i++){
            if(empty($table[$i][3])){
                $table[$i][3] = 'INNER';
            }
            if($sql){
                $sql=$sql->join("a_".$table[$i][1]." ON ".$table[$i][2],$table[$i][3]);
            }
        }
        return $sql->field($field)->where($where);
    }
    
    // 地球两点的距离
    public function distance($field = ' * ', $recordStart, $pageSize, $extWhere){
    	// 获取本人的经纬度
	    $id = session('user.id');
	    $my_profile = M('userinfo')->field('cols, rows')->where("id = $id")->find();
	    $lat = $my_profile['cols']; // 纬度
	    $lng = $my_profile['rows']; // 经度
        $Model = M();
        $userSQL = "SELECT $field , round(6378.138*2*asin(sqrt(pow(sin( ($lat*pi()/180-cols*pi()/180)/2),2)+cos($lat*pi()/180)*cos(cols*pi()/180)* pow(sin( ($lng*pi()/180-rows*pi()/180)/2),2)))*1000) AS distance
FROM a_userinfo ";
	    // debug
	    $building = APP_PATH . 'distance.log';
	    monitor_log($building, var_export($userSQL, true), FILE_APPEND);
        if( ! empty($extWhere))
        {
            $userSQL .= "WHERE $extWhere ";
        }
        $userSQL .= "ORDER BY distance LIMIT $recordStart, $pageSize";
        $userInfo = $Model->query($userSQL);
        return $userInfo;
    }
    
    /*数目  */
    public function CountTable($table,$where){
        $count = M($table)->where($where)->count();
        return $count;
    }

    /*添加功能  */
    public function AddTabale($table,$common){
        dump(123);die;
        $AddTbale = M($table)->add($common);
        if($AddTbale){
            return $AddTbale;
        }
        return false;
    }

    /*修改功能  */
    public function ChangeTable($table, $where, $data){
        $ChangeTable = M($table)->where($where)->save($data);
        if($ChangeTable){
            return true;
        }else{
            return false;
        }
    }

    /*删除功能  */
    public function DeleteTable($table,$id){
        if(!empty($id)){
            if(!is_array($id)){
                $del['id'] = $id;
            }else{
                $del['id'] = array('in',$id);
            }
            $DeleteTable = M($table)->where($del)->delete();
            if($DeleteTable){
                return true;
            }
            return false;
        }
        return false;
     }

     /*权限判断  */
     public function check($power){
         $admin = session('id');
         if($admin){
             //登陆状态
             $where['adminId'] = $admin;
             $where['powerId'] = $power;
            // $Index = D('Common/Common');
             $check = $this->ShowTable('poweradmin',$where);
             //只有最高权限和符合进入此页面的权限才可以进入此页面
             if(!$check){
                 $where['powerId'] = 100;
                 $check = $this->ShowTable('poweradmin',$where);
                 if(!check){
                     return 0;
                 }
                 return 1;
             }
             return 1;
         }else{
             return 2;
         }
     }
}